package studio.raptor.dispatcher.server.handler.dispatch;

import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import studio.raptor.dispatcher.common.constant.EventStatus;
import studio.raptor.dispatcher.server.context.HandlerContext;
import studio.raptor.dispatcher.server.handler.Handler;
import studio.raptor.dispatcher.server.service.EventService;
import studio.raptor.dispatcher.server.service.GraphService;
import studio.raptor.dispatcher.server.service.SendService;

/**
 * 消息分发处理
 *
 * @author jack
 * @since 1.0
 */
@Component
public class SendHanlder extends Handler {


  @Autowired
  SendService sendService;
  @Autowired
  GraphService graphService;
  @Autowired
  EventService eventService;

  @Override
  public boolean process(HandlerContext context) {
    Set<String> events = context.getEventIds();
    String code = context.getCode();
    Integer kindId = context.getKindId();
    Integer eventType = context.getEventType();
    for (String eventId : events) {
      graphService.updateEventStatus(code, eventId, EventStatus.PREPARE);
      eventService.updateEventTrace(code, eventId, kindId, EventStatus.PREPARE, -1, "sending");
      sendService.sendMessage(eventId, code, kindId, eventType, context.isSort());
    }
    return true;
  }

}
